﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using JYStockAnalyser.Face;
using JYStockAnalyser.Model;
using JYStockAnalyser.ClientLib;

namespace JYStockAnalyser.Manager
{
    /// <summary>
    /// 估值界面
    /// </summary>
    public partial class FrmEstimate : Form
    {
        Thread th;

        public FrmEstimate()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnEstimate_Click(object sender, EventArgs e)
        {
            m_startEstimate();
        }
     
        private void m_startEstimate()
        {
            th = new Thread(new ThreadStart(delegate()
            {
                StockManageFace face = new StockManageFace();
                YearQuarter beginQuarter = (YearQuarter)this.cbBeginQuarter.SelectedItem;
                YearQuarter endQuarter = (YearQuarter)this.cbEndQuarter.SelectedItem;

                if (beginQuarter == null || endQuarter == null)
                { Tool.ShowInformationBox("请输入起止季度"); return; }

                IEnumerable<YearQuarter> quaters = YearQuarter.GetCoveredYearQuarters(beginQuarter, endQuarter);

                foreach (YearQuarter quarter in quaters)
                {
                    int result = face.EstimateAndSave(quarter);
                    if (result != 1)
                    {
                        Tool.ShowInformationBox("估值失败!");
                        return;
                    }
                    else
                    {
                        this.lbStatus.Items.Add(String.Format("已经完成{0}的估值", quarter));
                    }
                }

                Tool.ShowInformationBox("估值成功!");
            }));
            th.Start();
        }

        private void FrmEstimate_Load(object sender, EventArgs e)
        {
            m_init();
        }

        void m_init()
        {
            YearQuarter[] maxMinYears = ClientCache.Instance.MinAndMaxQuarter;

            for (int year = maxMinYears[0].Year; year <= maxMinYears[1].Year; year++)
            {
                for (int quarter = 1; quarter <= 4; quarter++)
                {
                    if (year == maxMinYears[0].Year && quarter < maxMinYears[0].Quarter)
                    { continue; }

                    if (year == maxMinYears[1].Year && quarter > maxMinYears[1].Quarter)
                    { break; }

                    this.cbBeginQuarter.Items.Add(new YearQuarter(year, quarter));
                    this.cbEndQuarter.Items.Add(new YearQuarter(year, quarter));
                }
            }

            this.cbBeginQuarter.SelectedIndex = 0;
            this.cbEndQuarter.SelectedIndex = this.cbEndQuarter.Items.Count - 1;
        }
    }
}
